package com.beimihu.leetcode.哈希表;

import java.util.HashMap;

public class DuplicateElem219 {
//    示例 1：
//
//    输入：nums = [1,2,3,1], k = 3
//    输出：true
//    示例 2：
//
//    输入：nums = [1,0,1,1], k = 1
//    输出：true
//    示例 3：
//
//    输入：nums = [1,2,3,1,2,3], k = 2
//    输出：false
    // https://leetcode.cn/problems/contains-duplicate-ii/description/?envType=study-plan-v2&envId=top-interview-150
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> hashMap = new HashMap<>();

        for (int i = 0 ; i < nums.length; i++){
            if(!hashMap.containsKey(nums[i])){
                hashMap.put(nums[i],0);
            }
            else{
               int num =  hashMap.get(nums[i]);
                if(Math.abs(num - i) <=k ){
                    return true;
                }
            }

        }
        return false;
    }

    public static void main(String[] args) {
        int nums[] = {1,0,1,1};
        int k = 1;
        containsNearbyDuplicate(nums,k);
    }
}
